package com.cloudxingkong.dianyinke.SlideTimerShaft;

/**
 * Created by ningfei on 2016/5/26.
 */

import android.content.Context;
import android.widget.FrameLayout;
import android.widget.NumberPicker;

import com.cloudxingkong.dianyinke.R;

import java.util.Calendar;
public class YMDDatePicker extends FrameLayout {
    private final NumberPicker mYearPicker, mMonthPicker, mDayPicker;
    private Calendar mDate;
    private OnDateTimeChangedListener mOnDateTimeChangedListener;

    public YMDDatePicker(Context context) {
        super(context);
        mDate = Calendar.getInstance();
        inflate(context, R.layout.dialog_date_ymd, this);
        mYearPicker = (NumberPicker) this.findViewById(R.id.ymd_year);
        mMonthPicker = (NumberPicker) this.findViewById(R.id.ymd_month);
        mDayPicker = (NumberPicker) this.findViewById(R.id.ymd_day);
        mMonthPicker.setMinValue(1);
        mMonthPicker.setMaxValue(12);
        mYearPicker.setMinValue(1900);
        mYearPicker.setMaxValue(mDate.get(Calendar.YEAR) + 1000);
        mDayPicker.setMinValue(1);
        mDate.setTimeInMillis(System.currentTimeMillis());
        updateDateControl();
        mYearPicker.setOnValueChangedListener(mOnDateChangedListener);
        mMonthPicker.setOnValueChangedListener(mOnDateChangedListener);
        mDayPicker.setOnValueChangedListener(mOnDateChangedListener);
    }

    private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            if (picker.getId() == R.id.ymd_year) {
                mDate.add(Calendar.YEAR, newVal - oldVal);
            } else if (picker.getId() == R.id.ymd_month) {
                mDate.add(Calendar.MONTH, newVal - oldVal);
            } else if (picker.getId() == R.id.ymd_day) {
                mDate.add(Calendar.DAY_OF_MONTH, newVal - oldVal);
            }
            updateDateControl();
            onDateTimeChanged();
        }
    };

    private void updateDateControl() {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(mDate.getTimeInMillis());
        mDayPicker.setMaxValue(cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        mMonthPicker.setDisplayedValues(null);
        mDayPicker.setDisplayedValues(null);
        mMonthPicker.setValue(cal.get(Calendar.MONTH) + 1);
        mMonthPicker.invalidate();
        mDayPicker.setValue(cal.get(Calendar.DAY_OF_MONTH));
        mDayPicker.invalidate();
        mYearPicker.setDisplayedValues(null);
        mYearPicker.setValue(cal.get(Calendar.YEAR));
        mYearPicker.invalidate();
    }

    public interface OnDateTimeChangedListener {
        void onDateTimeChanged(YMDDatePicker view, int year, int month, int day);
    }

    public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) {
        mOnDateTimeChangedListener = callback;
    }

    private void onDateTimeChanged() {
        if (mOnDateTimeChangedListener != null) {
            mOnDateTimeChangedListener.onDateTimeChanged(this, mDate.get(Calendar.YEAR), mDate.get(Calendar.MONTH), mDate.get(Calendar.DAY_OF_MONTH));
        }
    }
}
